import {getShopCartHistory,setShopCartHistory,setUser} from '../utils/searchHistory'
import {getAddressList,placeOrders} from '@src/api/getData'
export default {
    saveGeohash({ commit },h){
        commit('SAVE_GEOHASH',h);
    },
    saveLoaction({ commit },local){
        commit('RECORD_ADDRESS',local);
    },
    saveShopId({commit},id){
        commit('RECORD_SHOPID',id);
    },
    setUserInfo({ commit },userinfo){
        commit('RECORD_USERINFO',userinfo);
        setUser(userinfo)
    },
    clearUserInfo({ commit }){
        commit('RECORD_USERINFO',null);
    },
    changeUserInfo({ commit ,state},u){
        let user=Object.assign({},state.user,{username:u})
        commit('RECORD_USERINFO',user);
    },
    saveAddressName({ commit },value){
        commit('SAVE_ADDDETAIL',value);
    },
    saveAddress({ commit ,state},value){
        commit('ADD_ADDRESS', value);
    },
    async saveAddressList({commit,state }){
        if(state.removeAddress.length > 0) return;
		let addres = await getAddressList(state.user.user_id);
		commit('SAVE_ADDRESS', addres);	
    },
    saveShopDetail({ commit ,state},value){
        commit('RECORD_SHOPDETAIL', value);	
    },
    getShopCart({ commit }){
        // request shopCart；
        // local shopCart;
        let list = getShopCartHistory();
        list? commit('INIT_BUYCART', list):'';
    },
    addCart({ commit ,state},{
        shopid,
		category_id,
		item_id,
		food_id,
		name,
		price,
		specs,
		packing_fee,
		sku_id,
		stock
    }){
        let cart = state.cartList;
        let shop = cart[shopid] = (cart[shopid] || {});
        let category = shop[category_id] = (shop[category_id] || {});
        let item = category[item_id] = (category[item_id] || {});
        if(item[food_id]){
            item[food_id]['num']++;
        }else{
            item[food_id]={
                "num" : 1,
                "category_id":category_id,
                "item_id":item_id,
                "food_id" : food_id,
                "id" : food_id,
                "name" : name,
                "price" : price,
                "specs" : specs,
                "packing_fee" : packing_fee,
                "sku_id" : sku_id,
                "stock" : stock
            }
        }
        commit('INIT_BUYCART',{...cart})
        //存入localStorage
		setShopCartHistory({...cart});
    },
    reduceCart({ commit ,state},{
        shopid,
		category_id,
		item_id,
		food_id,
		name,
		price,
		specs,
		packing_fee,
		sku_id,
		stock
    }){
        let cart = state.cartList;
        let shop = cart[shopid] = (cart[shopid] || {});
        let category = shop[category_id] = (shop[category_id] || {});
        let item = category[item_id] = (category[item_id] || {});
        if(item[food_id]){
            item[food_id]['num']--;
            if(item[food_id]['num'] === 0){
                delete item[food_id];
            }
        }
        commit('INIT_BUYCART',{...cart})
        //存入localStorage
		setShopCartHistory({...cart});
    },
    clearCart({commit,state},shopId){
        let cart = {...state.cartList};
        delete cart[shopId];
        commit('INIT_BUYCART',cart)
        //存入localStorage
		setShopCartHistory(cart);
    },
    saveCartID({commit},v){
        commit('SAVE_CART_ID_SIG',v);
    },
    async getChooseAddress({commit,state}){
        if(state.user && state.user.user_id){
            const addresRes = await getAddressList(state.user.user_id);
            if(addresRes instanceof Array  && addresRes.length){
                commit('CHOOSE_ADDR',{address:addresRes[0],index:0})
            }
        }
    },
    chooseAddress({commit,state},addr){
        commit('CHOOSE_ADDR',addr)
    },
    chooseSearchAddress({commit,state},addr){
        commit('CHOOSE_SEARCH_ADDR',addr)
    },
    addNewAddress({commit,state}){
        commit('CONFIRM_ADDRESS',1)
    },
    submitRemark({commit},v){
        commit('REMARKS',v)
    },
    submitOrder({commit},v){
        commit('ORDER',v);
        return placeOrders(v.user_id, v.cart_id, v.address_id, v.description, 
            v.entities, v.geohash, v.sig);
    },
    saveQuestionContent({commit},v){
        commit('QUESTION',v)
    }

}